#include "QQFileView.h"

QQFileView::QQFileView(QWidget* parent)
	: QQContentDialog(parent)
{
	QPalette palette = this->palette();
	palette.setColor(QPalette::Window, Qt::white);
	this->setPalette(palette);
	this->setAutoFillBackground(true);
	this->setFixedWidth(300);
	this->setMiddleButtonVisible(false);

	mTitle = new QLabel(this);
	mTitle->setMinimumHeight(30);

	mModel = new QStandardItemModel(this);
	mModel->setColumnCount(1);
	mListView = new ElaListView(this);
	mListView->setIsTransparent(true);
	mListView->setFixedHeight(250);
	mListView->setSelectionMode(QAbstractItemView::NoSelection);
	mListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	mListView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	mFileDelegate = new QQFileDelegate(this);
	mFileDelegate->setItemHeight(60);
	mListView->setModel(mModel);
	mListView->setItemDelegate(mFileDelegate);

	this->setLeftButtonText("Send");
	this->setMiddleButtonText("Cancel");
	this->setRightButtonText("Exit");

	QWidget* widget = new QWidget(this);
	QVBoxLayout* layout = new QVBoxLayout(widget);
	layout->setContentsMargins(0, 0, 0, 0);
	layout->setSpacing(0);
	layout->addWidget(mTitle);
	layout->addWidget(mListView);
	widget->setLayout(layout);
	this->setCentralWidget(widget);

	connect(this, &QQContentDialog::leftButtonClicked, this, &QQFileView::accept);
	connect(this, &QQContentDialog::leftButtonClicked, this, &QQFileView::doSendFiles);
	connect(this, &QQContentDialog::rightButtonClicked, this, &QQFileView::reject);
	connect(this, &QQContentDialog::rightButtonClicked, this, &QQFileView::doCancelSendFiles);
}

QQFileView::~QQFileView()
{
}

void QQFileView::setTitle(const QString& title)
{
	mTitle->setText(title);
}

void QQFileView::loadFile(const QFileInfo& fileInfo)
{
	mModel->clear();
	QStandardItem* item = new QStandardItem;
	QPixmap pixmap = mIconProvider.icon(fileInfo).pixmap(48, 48);
	item->setData(pixmap, Qt::DecorationRole);
	item->setData(fileInfo.fileName(), Qt::DisplayRole);
	item->setData(fileInfo.size(), Qt::UserRole);
	mModel->setItem(0, 0, item);
}

void QQFileView::loadFile(const QString& title, const QFileInfo& fileInfo)
{
	setTitle(title);
	loadFile(fileInfo);
}

void QQFileView::loadFiles(const QFileInfoList& fileInfos)
{
	mModel->clear();
	mModel->insertRows(0, fileInfos.size());
	for (int i = 0; i < fileInfos.size(); i++)
	{
		QStandardItem* item = new QStandardItem;
		QPixmap pixmap = mIconProvider.icon(fileInfos[i]).pixmap(48, 48);
		item->setData(pixmap, Qt::DecorationRole);
		item->setData(fileInfos[i].fileName(), Qt::DisplayRole);
		item->setData(fileInfos[i].size(), Qt::UserRole);
		mModel->setItem(i, item);
	}
}

void QQFileView::loadFiles(const QString& title, const QFileInfoList& fileInfos)
{
	setTitle(title);
	loadFiles(fileInfos);
}

void QQFileView::doSendFiles()
{
	mModel->clear();
	this->hide();
}

void QQFileView::doCancelSendFiles()
{
	mModel->clear();
	this->hide();
}